/*---------------------- 引入资源 ---------------------- */
import axios from "axios"
import NProgress from "nprogress"
/*---------------------- 获取token ---------------------- */
const getToken = () => {
    let str = sessionStorage.getItem('user-login')
    if (!str) {
        return 'token'
    }
    return JSON.parse(str).data.token
}
/*---------------------- axios的配置 ---------------------- */
// axios.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1/"
axios.defaults.baseURL = "http://146.56.230.35:8888/api/private/v1/"
axios.interceptors.request.use((req) => {
    NProgress.start()
    req.headers['Authorization'] = getToken()
    return req
})
axios.interceptors.response.use((res) => {
    NProgress.done()
    return res
})
const get = (opt) => {
    return axios.get(opt.url, {
        params: opt.data
    })
}
const post = (opt) => {
    return axios.post(opt.url, opt.data)
}
const del = (opt) => {
    return axios.delete(opt.url, {
        params: opt.data
    })
}
const put = (opt) => {
    return axios.put(opt.url, opt.data)
}
const map = new Map([
    ['get', get],
    ['post', post],
    ['delete', del],
    ['put', put]
])
/*---------------------- 导出请求方法  ---------------------- */
export default (opt) => {
    const mt = map.get(opt.type)
    if (!mt) {
        return new Promise(v => {
            v({
                data: null,
                meta: {
                    status: 404,
                    mag: '没有此' + opt.type + '类型方法'
                }
            })
        })
    }
    return mt(opt)
}